package com.webserver.servlet;




import com.webserver.http.HttpRequest;
import com.webserver.http.HttpRseponse;

import java.io.File;

/**
 * 用于完成处理请求环节的操作
 */
public class DispatcherServlet {

    public void service(HttpRequest request,HttpRseponse response){
        String path = request.getRequestURI();
        System.out.println("抽象路径为:"+path);
        if("/myweb/regUser".equals(path)){
            RegServlet servlet = new RegServlet();
            servlet.service(request, response);
        }else {
            File file = new File("./webapps" + path);
            //如果该资源存在,并且是一个文件则响应文件
            if (file.isFile() && file.exists()) {
                response.setEntity(file);
            } else {//否则响应404
                response.setStatusCode(404);
                response.setStatusReason("NotFound");
                file = new File("./webapps/root/404.html");
                response.setEntity(file);

                System.out.println("抽象路径为:./webapps/root/404.html");
            }
        }
        //Server响应头用来告知浏览器,服务端是谁
        response.putHeader("Server","WebServer");
    }
}
